<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\common;
use think\facade\Db;
class Fenxiao
{
    public static function fenxiao($sysset,$member,$product,$num,$commission_totalprice,$isfg=0,$istc1=0,$istc2=0,$istc3=0){
        $ogupdate = [];
        if($product['commissionset']!=-1){
            //开启紧缩，团队分红极差作为开关
            if(getcustom('commission_jinsuo') && $sysset['fx_jinsuo']==1){
                //var_dump($member['path']);
                if($member['path']){
                    $parentList = Db::name('member')->where('id','in',$member['path'])->order(Db::raw('field(id,'.$member['path'].')'))->select()->toArray();
                    if($parentList){
                        $parentList = array_reverse($parentList);
                        $level_lists = Db::name('member_level')->where('aid',aid)->column('*','id');
                        $parent_arr = [];
                        $agleveldata_arr = [];
                        //循环推荐网体，按代数查找提成比例大于0的上级，提成为0的紧缩掉不算一代
                        $dai = 1;
                        foreach($parentList as $k=>$parent){
                            if($dai>3){
                                break;
                            }
                            $level_data = $level_lists[$parent['levelid']]??[];
                            //没级别 紧缩掉
                            if(!$level_data){
                                //var_dump($parent['id'].'紧缩1');
                                continue;
                            }
                            //未开启分销权限，或下单会员不在指定等级ID中紧缩掉
                            if($level_data['can_agent']==0 || ($level_data['commission_appointlevelid'] && !in_array($member['levelid'],explode(',',$level_data['commission_appointlevelid'])))){
                                //var_dump($parent['id'].'紧缩2');
                                continue;
                            }
                            //提成比例为0的紧缩掉(应客户要求，只按一代比例做判断)
                            //$commission_dai = $level_data['commission'.$dai];//(此处为按会员当前所处层级的拿奖比例做判断)
                            $commission_dai = $level_data['commission1'];
                            $commission_socre = 0;
                            if(getcustom('maidan_commission_score')){
                                $commission_socre = $level_data['maidan_commission_score1'];
                            }

                            if($commission_dai<=0 && $commission_socre<=0){
                                //var_dump($parent['id'].'紧缩3');
                                continue;
                            }

                            $parent_arr[$dai] = $parent;
                            $agleveldata_arr[$dai] = $level_data;
                            $dai++;
                        }
                        //上一代会员
                        $parent1 = $parent_arr[1]??'';
                        $agleveldata1 = $agleveldata_arr[1]??'';
                        $ogupdate['parent1'] = $parent1['id']??0;
                        //上二代会员
                        $parent2 = $parent_arr[2]??'';
                        $agleveldata2 = $agleveldata_arr[2]??'';
                        $ogupdate['parent2'] = $parent2['id']??0;
                        //上三代会员
                        $parent3 = $parent_arr[3]??'';
                        $agleveldata3 = $agleveldata_arr[3]??'';
                        $ogupdate['parent3'] = $parent3['id']??0;

                    }
                }
            }else{
                if($member['pid']){
                    $parent1 = Db::name('member')->where('aid',aid)->where('id',$member['pid'])->find();
                    if($parent1){
                        $agleveldata1 = Db::name('member_level')->where('aid',aid)->where('id',$parent1['levelid'])->find();
                        if($agleveldata1['can_agent']!=0 && (!$agleveldata1['commission_appointlevelid'] || in_array($member['levelid'],explode(',',$agleveldata1['commission_appointlevelid'])))){
                            $ogupdate['parent1'] = $parent1['id'];
                        }
                    }
                }
                if($parent1['pid']){
                    $parent2 = Db::name('member')->where('aid',aid)->where('id',$parent1['pid'])->find();
                    if($parent2){
                        $agleveldata2 = Db::name('member_level')->where('aid',aid)->where('id',$parent2['levelid'])->find();
                        if($agleveldata2['can_agent']>1 && (!$agleveldata2['commission_appointlevelid'] || in_array($member['levelid'],explode(',',$agleveldata2['commission_appointlevelid'])))){
                            $ogupdate['parent2'] = $parent2['id'];
                        }
                    }
                }
                if($parent2['pid']){
                    $parent3 = Db::name('member')->where('aid',aid)->where('id',$parent2['pid'])->find();
                    if($parent3){
                        $agleveldata3 = Db::name('member_level')->where('aid',aid)->where('id',$parent3['levelid'])->find();
                        if($agleveldata3['can_agent']>2 && (!$agleveldata3['commission_appointlevelid'] || in_array($member['levelid'],explode(',',$agleveldata3['commission_appointlevelid'])))){
                            $ogupdate['parent3'] = $parent3['id'];
                        }
                    }
                }
            }

            if($parent3['pid']){
                $parent4 = Db::name('member')->where('aid',aid)->where('id',$parent3['pid'])->find();
                if($parent4){
                    $agleveldata4 = Db::name('member_level')->where('aid',aid)->where('id',$parent4['levelid'])->find();
                    if($product['commissionpingjiset'] != 0){
                        if($product['commissionpingjiset'] == 1){
                            $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                            $agleveldata4['commission_parent_pj'] = $commissionpingjidata1[$agleveldata4['id']];
                        }elseif($product['commissionpingjiset'] == 2){
                            $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                            $agleveldata4['commission_parent_pj'] = $commissionpingjidata2[$agleveldata4['id']];
                        }else{
                            $agleveldata4['commission_parent_pj'] = 0;
                        }
                    }
                    //持续推荐奖励
                    if($agleveldata4['can_agent'] > 0 && ($agleveldata4['commission_parent'] > 0 || ($parent4['levelid']==$parent3['levelid'] && $agleveldata4['commission_parent_pj'] > 0))){
                        $ogupdate['parent4'] = $parent4['id'];
                    }
                }
            }
            if($product['commissionset']==1){//按商品设置的分销比例
                $commissiondata = json_decode($product['commissiondata1'],true);
                if($commissiondata){
                    if($agleveldata1) $ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $commission_totalprice * 0.01;
                    if($agleveldata2) $ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $commission_totalprice * 0.01;
                    if($agleveldata3) $ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $commission_totalprice * 0.01;
                    if(getcustom('commission_butie')){
                        $commissionbutie = json_decode($product['commissionbutie2'],true);
                        if($agleveldata1) $ogupdate['parent1commission_butie'] = $commissionbutie[$agleveldata1['id']]['commission1'] * $ogupdate['parent1commission'] * 0.01;
                        if($agleveldata2) $ogupdate['parent2commission_butie'] = $commissionbutie[$agleveldata2['id']]['commission2'] * $ogupdate['parent2commission'] * 0.01;
                        if($agleveldata3) $ogupdate['parent3commission_butie'] = $commissionbutie[$agleveldata3['id']]['commission3'] * $ogupdate['parent3commission'] * 0.01;
                    }
                }
            }elseif($product['commissionset']==2){//按固定金额
                $commissiondata = json_decode($product['commissiondata2'],true);
                if($commissiondata){
                    if(getcustom('fengdanjiangli') && $product['fengdanjiangli']){

                    }else{
                        if($agleveldata1) $ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
                        if($agleveldata2) $ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
                        if($agleveldata3) $ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
                        if(getcustom('commission_butie')){
                            $commissionbutie = json_decode($product['commissionbutie'],true);
                            if($agleveldata1) $ogupdate['parent1commission_butie'] = $commissionbutie[$agleveldata1['id']]['commission1'];
                            if($agleveldata2) $ogupdate['parent2commission_butie'] = $commissionbutie[$agleveldata2['id']]['commission2'];
                            if($agleveldata3) $ogupdate['parent3commission_butie'] = $commissionbutie[$agleveldata3['id']]['commission3'];
                        }
                    }
                }
            }elseif($product['commissionset']==3){//提成是积分
                $commissiondata = json_decode($product['commissiondata3'],true);
                if($commissiondata){
                    if($agleveldata1) $ogupdate['parent1score'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
                    if($agleveldata2) $ogupdate['parent2score'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
                    if($agleveldata3) $ogupdate['parent3score'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
                }
            }elseif($product['commissionset']==5){//提成比例+积分
                $commissiondata = json_decode($product['commissiondata1'],true);
                if($commissiondata){
                    if($agleveldata1) $ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $commission_totalprice * 0.01;
                    if($agleveldata2) $ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $commission_totalprice * 0.01;
                    if($agleveldata3) $ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $commission_totalprice * 0.01;
                    if(getcustom('commission_butie')){
                        $commissionbutie = json_decode($product['commissionbutie2'],true);
                        if($agleveldata1) $ogupdate['parent1commission_butie'] = $commissionbutie[$agleveldata1['id']]['commission1'] * $ogupdate['parent1commission'] * 0.01;
                        if($agleveldata2) $ogupdate['parent2commission_butie'] = $commissionbutie[$agleveldata2['id']]['commission2'] * $ogupdate['parent2commission'] * 0.01;
                        if($agleveldata3) $ogupdate['parent3commission_butie'] = $commissionbutie[$agleveldata3['id']]['commission3'] * $ogupdate['parent3commission'] * 0.01;
                    }
                }
                $commissiondata = json_decode($product['commissiondata3'],true);
                if($commissiondata){
                    if($agleveldata1) $ogupdate['parent1score'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
                    if($agleveldata2) $ogupdate['parent2score'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
                    if($agleveldata3) $ogupdate['parent3score'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
                }
            }elseif($product['commissionset']==6){//提成金额+积分
                $commissiondata = json_decode($product['commissiondata2'],true);
                if($commissiondata){
                    if(getcustom('fengdanjiangli') && $product['fengdanjiangli']){

                    }else{
                        if($agleveldata1) $ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
                        if($agleveldata2) $ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
                        if($agleveldata3) $ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
                        if(getcustom('commission_butie')){
                            $commissionbutie = json_decode($product['commissionbutie'],true);
                            if($agleveldata1) $ogupdate['parent1commission_butie'] = $commissionbutie[$agleveldata1['id']]['commission1'];
                            if($agleveldata2) $ogupdate['parent2commission_butie'] = $commissionbutie[$agleveldata2['id']]['commission2'];
                            if($agleveldata3) $ogupdate['parent3commission_butie'] = $commissionbutie[$agleveldata3['id']]['commission3'];
                        }
                    }
                }
                $commissiondata = json_decode($product['commissiondata3'],true);
                if($commissiondata){
                    if($agleveldata1) $ogupdate['parent1score'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
                    if($agleveldata2) $ogupdate['parent2score'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
                    if($agleveldata3) $ogupdate['parent3score'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
                }
            }elseif($product['commissionset']==7){//提成积分比例
                $commissiondata = json_decode($product['commissiondata4'],true);
                if($commissiondata){
                    if($agleveldata1) $ogupdate['parent1score'] = round($commissiondata[$agleveldata1['id']]['commission1'] * $commission_totalprice * 0.01);
                    if($agleveldata2) $ogupdate['parent2score'] = round($commissiondata[$agleveldata2['id']]['commission2'] * $commission_totalprice * 0.01);
                    if($agleveldata3) $ogupdate['parent3score'] = round($commissiondata[$agleveldata3['id']]['commission3'] * $commission_totalprice * 0.01);
                }
            }else{ //按会员等级设置的分销比例
                if ($agleveldata1) {
                    if (getcustom('plug_ttdz') && $isfg == 1) {
                        $agleveldata1['commission1'] = $agleveldata1['commission4'];
                    }
                    if ($agleveldata1['commissiontype'] == 1) { //固定金额按单
                        if ($istc1 == 0) {
                            $ogupdate['parent1commission'] = $agleveldata1['commission1'];
                            $istc1 = 1;
                        }
                    } else {
                        $ogupdate['parent1commission'] = $agleveldata1['commission1'] * $commission_totalprice * 0.01;
                    }
                }
                if ($agleveldata2) {
                    if (getcustom('plug_ttdz') && $isfg == 1) {
                        $agleveldata2['commission2'] = $agleveldata2['commission5'];
                    }
                    if ($agleveldata2['commissiontype'] == 1) {
                        if ($istc2 == 0) {
                            $ogupdate['parent2commission'] = $agleveldata2['commission2'];
                            $istc2 = 1;
                            //持续推荐奖励
                            if ($agleveldata2['commission_parent'] > 0 && $ogupdate['parent1']) {
                                $ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent'];
                            }
                        }
                    } else {
                        $ogupdate['parent2commission'] = $agleveldata2['commission2'] * $commission_totalprice * 0.01;
                        //持续推荐奖励
                        if ($agleveldata2['commission_parent'] > 0 && $ogupdate['parent1commission'] > 0 && $ogupdate['parent1']) {
                            $ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent'] * 0.01;
                        }
                    }

                }
                if ($agleveldata3) {
                    if (getcustom('plug_ttdz') && $isfg == 1) {
                        $agleveldata3['commission3'] = $agleveldata3['commission6'];
                    }

                    if ($agleveldata3['commissiontype'] == 1) {
                        if ($istc3 == 0) {
                            $ogupdate['parent3commission'] = $agleveldata3['commission3'];
                            $istc3 = 1;
                            //持续推荐奖励
                            if ($agleveldata3['commission_parent'] > 0 && $ogupdate['parent2']) {
                                $ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent'];
                            }
                        }
                    } else {
                        $ogupdate['parent3commission'] = $agleveldata3['commission3'] * $commission_totalprice * 0.01;
                        //持续推荐奖励
                        if ($agleveldata3['commission_parent'] > 0 && $ogupdate['parent2commission'] > 0 && $ogupdate['parent2']) {
                            $ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent'] * 0.01;
                        }
                    }
                }
                //持续推荐奖励
                if ($agleveldata4['commission_parent'] > 0 && $ogupdate['parent3']) {
                    if ($agleveldata3['commissiontype'] == 1) {
                        $ogupdate['parent4commission'] = $agleveldata4['commission_parent'];
                    } else {
                        $ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent'] * 0.01;
                    }
                }

            }

            //平级奖
            if(getcustom('commission_parent_pj') && !getcustom('commission_parent_pj_stop')){
                if($agleveldata4 && $ogupdate['parent3'] && $ogupdate['parent3commission'] > 0 && $agleveldata3['id'] == $agleveldata4['id']){
                    $agleveldata4['commissionpingjitype'] = $agleveldata4['commissiontype'];
                    if($product['commissionpingjiset'] != 0){
                        if($product['commissionpingjiset'] == 1){
                            $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                            $agleveldata4['commission_parent_pj'] = $commissionpingjidata1[$agleveldata4['id']]['commission'];
                        }elseif($product['commissionpingjiset'] == 2){
                            $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                            $agleveldata4['commission_parent_pj'] = $commissionpingjidata2[$agleveldata4['id']]['commission'];
                            $agleveldata4['commissionpingjitype'] = 1;
                        }else{
                            $agleveldata4['commission_parent_pj'] = 0;
                        }
                    }
                    if($agleveldata4['commission_parent_pj'] > 0) {
                        if($agleveldata4['commissionpingjitype']==0){
                            $ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent_pj'] * 0.01;
                        } else {
                            $ogupdate['parent4commission'] = $agleveldata4['commission_parent_pj'];
                        }
                        $ogupdate['parent4'] = $parent4['id'];
                    }
                }
                if($agleveldata3 && $ogupdate['parent2'] && $ogupdate['parent2commission'] > 0 && $agleveldata2['id'] == $agleveldata3['id']){
                    $agleveldata3['commissionpingjitype'] = $agleveldata3['commissiontype'];
                    if($product['commissionpingjiset'] != 0){
                        if($product['commissionpingjiset'] == 1){
                            $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                            $agleveldata3['commission_parent_pj'] = $commissionpingjidata1[$agleveldata3['id']]['commission'];
                        }elseif($product['commissionpingjiset'] == 2){
                            $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                            $agleveldata3['commission_parent_pj'] = $commissionpingjidata2[$agleveldata3['id']]['commission'];
                            $agleveldata3['commissionpingjitype'] = 1;
                        }else{
                            $agleveldata3['commission_parent_pj'] = 0;
                        }
                    }
                    if($agleveldata3['commission_parent_pj'] > 0){
                        if(!$ogupdate['parent3']){
                            $ogupdate['parent3commission'] = 0;
                            $ogupdate['parent3'] = $parent3['id'];
                        }
                        if($agleveldata3['commissionpingjitype'] == 0){
                            $ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent_pj'] * 0.01;
                        }else{
                            $ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent_pj'];
                        }
                    }
                }
                if($agleveldata2 && $ogupdate['parent1'] && $ogupdate['parent1commission'] > 0 && $agleveldata1['id'] == $agleveldata2['id']){
                    $agleveldata2['commissionpingjitype'] = $agleveldata2['commissiontype'];
                    if($product['commissionpingjiset'] != 0){
                        if($product['commissionpingjiset'] == 1){
                            $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                            $agleveldata2['commission_parent_pj'] = $commissionpingjidata1[$agleveldata2['id']]['commission'];
                        }elseif($product['commissionpingjiset'] == 2){
                            $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                            $agleveldata2['commission_parent_pj'] = $commissionpingjidata2[$agleveldata2['id']]['commission'];
                            $agleveldata2['commissionpingjitype'] = 1;
                        }else{
                            $agleveldata2['commission_parent_pj'] = 0;
                        }
                    }
                    if($agleveldata2['commission_parent_pj'] > 0){
                        if(!$ogupdate['parent2']){
                            $ogupdate['parent2commission'] = 0;
                            $ogupdate['parent2'] = $parent2['id'];
                        }
                        if($agleveldata2['commissionpingjitype'] == 0){
                            $ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent_pj'] * 0.01;
                        }else{
                            $ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent_pj'];
                        }
                    }
                }
            }
            /******************************平级后只拿平级奖，不再向上发放分销奖金 start **************************************/
            $commission_parent_pj_stop_product = getcustom('commission_parent_pj_stop_product');//产品单独设置平级奖参数
            if(getcustom('commission_parent_pj_stop') && $member['path']){
                //重新计算平级奖
                $pids = $member['path'];
                $parentList = Db::name('member')->where('id','in',$pids)->order(Db::raw('field(id,'.$pids.')'))->select()->toArray();
                $parentList = array_reverse($parentList);
                $last_level_data = Db::name('member_level')->where('id',$member['levelid'])->find();
                $level_arr = Db::name('member_level')->where('aid',$sysset['aid'])->column('*','id');
                //产品单独设置平级奖参数
                if($commission_parent_pj_stop_product){
                    if($product['commission_parent_pj_status']==1){
                        //单独设置
                        foreach($level_arr as $k=>$v){
                            $level_arr[$k]['commission_parent_pj_status'] = 1;
                            $level_arr[$k]['commission_parent_pj_lv'] = $product['commission_parent_pj_lv'];
                            $level_arr[$k]['commission_parent_pj'] = $product['commission_parent_pj']*$num;
                            $level_arr[$k]['commission_parent_pj_order'] = $product['commission_parent_pj_order'];
                        }
                    }elseif ($product['commission_parent_pj_status']==-1){
                        //关闭平级奖
                        foreach($level_arr as $k=>$v){
                            $level_arr[$k]['commission_parent_pj_status'] = 0;
                        }
                    }
                }
                $i = 0;
                $pjlevelids = [];//已拿平级奖的会员
                foreach($parentList as $parentpj){
                    $i++;
                    if($i==1){
                        $commission_field = 'parent1commission';
                        $parent_field = 'parent1';
                    }elseif($i==2){
                        $commission_field = 'parent2commission';
                        $parent_field = 'parent2';
                    }elseif($i==3){
                        $commission_field = 'parent3commission';
                        $parent_field = 'parent3';
                    }else{
                        $commission_field = 'parent4commission';
                        $parent_field = 'parent4';
                    }
                    $level_data = $level_arr[$parentpj['levelid']];
                    if($level_data['commission_parent_pj_lv']>0 && $level_data['commission_parent_pj_lv']<$i){
                        //超出层级限制
                        if($level_data['id']==$last_level_data['id']){
                            $ogupdate[$commission_field] = 0;
                        }
                        //超出层级限制，跳过
                        continue;
                    }
//                    dump($i.'=>'.$parentpj['id'].'开始,级别'.$last_level_data['id'].'=>'.$level_data['id']);
                    if($level_data['commission_parent_pj_status']==1 && !in_array($level_data['id'],$pjlevelids)){
                        if($level_data['id']==$last_level_data['id']){
                            $pj_bonus = $level_data['commission_parent_pj'];//固定金额
                            $pj_bonus_order = bcmul($level_data['commission_parent_pj_order']/100,$commission_totalprice,2);
                            $pj_bonus = bcadd($pj_bonus,$pj_bonus_order,2);
                            $ogupdate[$commission_field] = $pj_bonus;
                            $ogupdate[$parent_field] = $parentpj['id'];
                            $pjlevelids = [$level_data['id']];
                            if($i>=4){
                                //平级奖只发最近的一个
                                break;
                            }
                        }
                        if($level_data['sort']<$last_level_data['sort']){
                            //设置了平级奖，如果上级会员级别小于下级，那么不发放分销佣金
                            $ogupdate[$commission_field] = 0;
                            continue;
                        }
                    }
                    if($i<4){
                        $last_level_data = $level_data;
                    }
                }
            }
            /******************************平级后只拿平级奖，不再向上发放分销奖金 stop **************************************/
//                    dump($ogupdate);exit;
        }
        $ogupdate['istc1'] = $istc1;
        $ogupdate['istc2'] = $istc2;
        $ogupdate['istc3'] = $istc3;
        return $ogupdate;
    }

    public static function fenxiao_jicha($sysset,$member,$product,$num,$commission_totalprice){
        if(getcustom('fenxiao_manage')){
        $fenxiao_set = Db::name('fenxiao_manage')->where('aid',$sysset['aid'])->column('*','key');
        $commission1_bili = 0;
        $commission2_bili = 0;
        $commission3_bili = 0;
        $ogupdate = [];
        if($member['pid']){
            $parent1 = Db::name('member')->where('aid',aid)->where('id',$member['pid'])->find();
            if($parent1){
                $agleveldata1 = Db::name('member_level')->where('aid',aid)->where('id',$parent1['levelid'])->find();
                if($agleveldata1['can_agent']!=0 ){
                    $ogupdate['parent1'] = $parent1['id'];
                    $set_key = $parent1['levelid'].','.$member['levelid'];
                    $commission1_bili = $fenxiao_set[$set_key]['commission1'];
                    //dump($set_key.'=>'.$commission1_bili);
                }
            }
        }
        if($parent1['pid']){
            $parent2 = Db::name('member')->where('aid',aid)->where('id',$parent1['pid'])->find();
            if($parent2){
                $agleveldata2 = Db::name('member_level')->where('aid',aid)->where('id',$parent2['levelid'])->find();
                if($agleveldata2['can_agent']>1){
                    $ogupdate['parent2'] = $parent2['id'];
                    $set_key = $parent2['levelid'].','.$member['levelid'];
                    $commission2_bili = $fenxiao_set[$set_key]['commission2'];
                    //dump($set_key.'=>'.$commission2_bili);
                }
            }
        }
        if($parent2['pid']){
            $parent3 = Db::name('member')->where('aid',aid)->where('id',$parent2['pid'])->find();
            if($parent3){
                $agleveldata3 = Db::name('member_level')->where('aid',aid)->where('id',$parent3['levelid'])->find();
                if($agleveldata3['can_agent']>2 ){
                    $ogupdate['parent3'] = $parent3['id'];
                    $set_key = $parent3['levelid'].','.$member['levelid'];
                    $commission3_bili = $fenxiao_set[$set_key]['commission3'];
                    //dump($set_key.'=>'.$commission3_bili);
                }
            }
        }
        if($parent3['pid']){
            $parent4 = Db::name('member')->where('aid',aid)->where('id',$parent3['pid'])->find();
            if($parent4){
                $agleveldata4 = Db::name('member_level')->where('aid',aid)->where('id',$parent4['levelid'])->find();
                if($product['commissionpingjiset'] != 0){
                    if($product['commissionpingjiset'] == 1){
                        $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                        $agleveldata4['commission_parent_pj'] = $commissionpingjidata1[$agleveldata4['id']];
                    }elseif($product['commissionpingjiset'] == 2){
                        $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                        $agleveldata4['commission_parent_pj'] = $commissionpingjidata2[$agleveldata4['id']];
                    }else{
                        $agleveldata4['commission_parent_pj'] = 0;
                    }
                }
                //持续推荐奖励
                if($agleveldata4['can_agent'] > 0){
                    $ogupdate['parent4'] = $parent4['id'];
                }
            }
        }
        $last_commission_bili = 0;//记录上一次分销奖金比例，计算级差使用
        //按会员等级设置的分销比例
        if ($agleveldata1) {
            $commission_bili = bcsub($commission1_bili,$last_commission_bili,2);
            $last_commission_bili = $commission_bili;
            //dump('parent1commission'.'=>'.$commission_bili);
            $ogupdate['parent1commission'] = bcmul($commission_bili/100,$commission_totalprice,2);
        }
        if ($agleveldata2) {
            $commission_bili = bcsub($commission2_bili,$last_commission_bili,2);
            if($commission_bili<0){
                $commission_bili = 0;
            }else{
                $last_commission_bili = $commission2_bili;
            }
            //dump('parent2commission'.'=>'.$commission2_bili.'=>'.$commission_bili);
            $ogupdate['parent2commission'] = bcmul($commission_bili/100,$commission_totalprice,2);

        }
        if ($agleveldata3) {
            $commission_bili = bcsub($commission3_bili,$last_commission_bili,2);
            if($commission_bili<0){
                $commission_bili = 0;
            }else{
                $last_commission_bili = $commission3_bili;
            }
            //dump('parent2commission'.'=>'.$commission3_bili.'=>'.$commission_bili);
            $ogupdate['parent3commission'] = bcmul($commission_bili/100,$commission_totalprice,2);
        }

        //平级奖
        if(getcustom('commission_parent_pj') && !getcustom('commission_parent_pj_stop')){
            if($agleveldata4 && $ogupdate['parent3'] && $ogupdate['parent3commission'] > 0 && $agleveldata3['id'] == $agleveldata4['id']){
                $agleveldata4['commissionpingjitype'] = $agleveldata4['commissiontype'];
                if($product['commissionpingjiset'] != 0){
                    if($product['commissionpingjiset'] == 1){
                        $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                        $agleveldata4['commission_parent_pj'] = $commissionpingjidata1[$agleveldata4['id']]['commission'];
                    }elseif($product['commissionpingjiset'] == 2){
                        $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                        $agleveldata4['commission_parent_pj'] = $commissionpingjidata2[$agleveldata4['id']]['commission'];
                        $agleveldata4['commissionpingjitype'] = 1;
                    }else{
                        $agleveldata4['commission_parent_pj'] = 0;
                    }
                }
                if($agleveldata4['commission_parent_pj'] > 0) {
                    if($agleveldata4['commissionpingjitype']==0){
                        $ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent_pj'] * 0.01;
                    } else {
                        $ogupdate['parent4commission'] = $agleveldata4['commission_parent_pj'];
                    }
                    $ogupdate['parent4'] = $parent4['id'];
                }
            }
            if($agleveldata3 && $ogupdate['parent2'] && $ogupdate['parent2commission'] > 0 && $agleveldata2['id'] == $agleveldata3['id']){
                $agleveldata3['commissionpingjitype'] = $agleveldata3['commissiontype'];
                if($product['commissionpingjiset'] != 0){
                    if($product['commissionpingjiset'] == 1){
                        $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                        $agleveldata3['commission_parent_pj'] = $commissionpingjidata1[$agleveldata3['id']]['commission'];
                    }elseif($product['commissionpingjiset'] == 2){
                        $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                        $agleveldata3['commission_parent_pj'] = $commissionpingjidata2[$agleveldata3['id']]['commission'];
                        $agleveldata3['commissionpingjitype'] = 1;
                    }else{
                        $agleveldata3['commission_parent_pj'] = 0;
                    }
                }
                if($agleveldata3['commission_parent_pj'] > 0){
                    if(!$ogupdate['parent3']){
                        $ogupdate['parent3commission'] = 0;
                        $ogupdate['parent3'] = $parent3['id'];
                    }
                    if($agleveldata3['commissionpingjitype'] == 0){
                        $ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent_pj'] * 0.01;
                    }else{
                        $ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent_pj'];
                    }
                }
            }
            if($agleveldata2 && $ogupdate['parent1'] && $ogupdate['parent1commission'] > 0 && $agleveldata1['id'] == $agleveldata2['id']){
                $agleveldata2['commissionpingjitype'] = $agleveldata2['commissiontype'];
                if($product['commissionpingjiset'] != 0){
                    if($product['commissionpingjiset'] == 1){
                        $commissionpingjidata1 = json_decode($product['commissionpingjidata1'],true);
                        $agleveldata2['commission_parent_pj'] = $commissionpingjidata1[$agleveldata2['id']]['commission'];
                    }elseif($product['commissionpingjiset'] == 2){
                        $commissionpingjidata2 = json_decode($product['commissionpingjidata2'],true);
                        $agleveldata2['commission_parent_pj'] = $commissionpingjidata2[$agleveldata2['id']]['commission'];
                        $agleveldata2['commissionpingjitype'] = 1;
                    }else{
                        $agleveldata2['commission_parent_pj'] = 0;
                    }
                }
                if($agleveldata2['commission_parent_pj'] > 0){
                    if(!$ogupdate['parent2']){
                        $ogupdate['parent2commission'] = 0;
                        $ogupdate['parent2'] = $parent2['id'];
                    }
                    if($agleveldata2['commissionpingjitype'] == 0){
                        $ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent_pj'] * 0.01;
                    }else{
                        $ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent_pj'];
                    }
                }
            }
        }
        /******************************平级后只拿平级奖，不再向上发放分销奖金 start **************************************/
        if(getcustom('commission_parent_pj_stop') && $member['path']){
            //重新计算平级奖
            $pids = $member['path'];
            $parentList = Db::name('member')->where('id','in',$pids)->order(Db::raw('field(id,'.$pids.')'))->select()->toArray();
            $parentList = array_reverse($parentList);
            $last_level_data = Db::name('member_level')->where('id',$member['levelid'])->find();
            $i = 0;
            $pjlevelids = [];//已拿平级奖的会员
            foreach($parentList as $parentpj){
                $i++;
                if($i==1){
                    $commission_field = 'parent1commission';
                    $parent_field = 'parent1';
                }elseif($i==2){
                    $commission_field = 'parent2commission';
                    $parent_field = 'parent2';
                }elseif($i==3){
                    $commission_field = 'parent3commission';
                    $parent_field = 'parent3';
                }else{
                    $commission_field = 'parent4commission';
                    $parent_field = 'parent4';
                }
                $level_data = Db::name('member_level')->where('id',$parentpj['levelid'])->find();
                if($level_data['commission_parent_pj_lv']>0 && $level_data['commission_parent_pj_lv']<$i){
                    //超出层级限制
                    if($level_data['id']==$last_level_data['id']){
                        $ogupdate[$commission_field] = 0;
                    }
                    //超出层级限制，跳过
                    continue;
                }
                //dump($i.'=>'.$parentpj['id'].'开始,级别'.$last_level_data['id'].'=>'.$level_data['id']);
                if($level_data['commission_parent_pj_status']==1 && !in_array($level_data['id'],$pjlevelids)){
                    if($level_data['id']==$last_level_data['id']){
                        $pj_bonus = $level_data['commission_parent_pj'];//固定金额
                        $pj_bonus_order = bcmul($level_data['commission_parent_pj_order']/100,$commission_totalprice,2);
                        $pj_bonus = bcadd($pj_bonus,$pj_bonus_order,2);
                        $ogupdate[$commission_field] = $pj_bonus;
                        $ogupdate[$parent_field] = $parentpj['id'];
                        $pjlevelids = [$level_data['id']];
                        if($i>=4){
                            //平级奖只发最近的一个
                            break;
                        }
                    }
                    if($level_data['sort']<$last_level_data['sort']){
                        //设置了平级奖，如果上级会员级别小于下级，那么不发放分销佣金
                        $ogupdate[$commission_field] = 0;
                        continue;
                    }
                }
                if($i<4){
                    $last_level_data = $level_data;
                }
            }
        }
        /******************************平级后只拿平级奖，不再向上发放分销奖金 stop **************************************/
//                    dump($ogupdate);exit;
        return $ogupdate;
        }
    }
}